﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RM2012Server.Model;
using System.Data;
using RM2012Server.Business;
using DevComponents.DotNetBar.Controls;
using System.Windows.Forms;

namespace RM2012Server.Controller
{
    public class UserCtrl
    {
        UserDAO m_UserDAO = new UserDAO();

        public void LoadToComboBox(ComboBoxEx cmb)
        {
            cmb.DataSource = GetAll();
            cmb.DisplayMember = "FullName";
            cmb.ValueMember = "UserId";
        }

        public void LoadToDataGridViewComboBoxColumn(DataGridViewComboBoxColumn cmb)
        {
            cmb.DataSource = GetAll();
            cmb.DisplayMember = "FullName";
            cmb.ValueMember = "UserId";
            cmb.DataPropertyName = "UserId";
            cmb.HeaderText = "Người dùng";
        }

        public void LoadToDataGridViewComboBoxExColumn(DataGridViewComboBoxExColumn cmb)
        {
            cmb.DataSource = GetAll();
            cmb.DisplayMember = "FullName";
            cmb.ValueMember = "UserId";
            cmb.DataPropertyName = "UserId";
            cmb.HeaderText = "Người dùng";
        }

        public void Show(DataGridViewX dgv,
                         BindingNavigator bn,
                         TextBoxX txtUserId,
                         TextBoxX txtFullName,
                         CheckBoxX rbSex,
                         CheckBoxX rbMale,
                         CheckBoxX rbFemale,
                         TextBoxX txtAddress,
                         TextBoxX txtPhone,
                         TextBoxX txtEmail,
                         TextBoxX txtUserName,
                         TextBoxX txtSalary,
                         TextBoxX txtRemark)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = GetAll();

            txtUserId.DataBindings.Clear();
            txtUserId.DataBindings.Add("Text", bs, "UserId");

            txtFullName.DataBindings.Clear();
            txtFullName.DataBindings.Add("Text", bs, "FullName");

            rbSex.DataBindings.Clear();
            rbSex.DataBindings.Add("Checked", bs, "Sex");

            if (rbSex.Checked == true)
            {
                rbFemale.Checked = true;
                rbMale.Checked = false;
            }
            else
            {
                rbFemale.Checked = false;
                rbMale.Checked = true;
            }

            txtAddress.DataBindings.Clear();
            txtAddress.DataBindings.Add("Text", bs, "Address");

            txtPhone.DataBindings.Clear();
            txtPhone.DataBindings.Add("Text", bs, "Phone");

            txtEmail.DataBindings.Clear();
            txtEmail.DataBindings.Add("Text", bs, "Email");

            txtUserName.DataBindings.Clear();
            txtUserName.DataBindings.Add("Text", bs, "UserName");

            txtSalary.DataBindings.Clear();
            txtSalary.DataBindings.Add("Text", bs, "Salary");

            txtRemark.DataBindings.Clear();
            txtRemark.DataBindings.Add("Text", bs, "Remark");

            bn.BindingSource = bs;
            dgv.DataSource = bs;
        }

        public void ShowWithSource(DataGridViewX dgv,
                         BindingNavigator bn,
                         TextBoxX txtUserId,
                         TextBoxX txtFullName,
                         CheckBoxX rbSex,
                         CheckBoxX rbMale,
                         CheckBoxX rbFemale,
                         TextBoxX txtAddress,
                         TextBoxX txtPhone,
                         TextBoxX txtEmail,
                         TextBoxX txtUserName,
                         TextBoxX txtSalary,
                         TextBoxX txtRemark,
                         DataTable dtSources)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = dtSources;

            txtUserId.DataBindings.Clear();
            txtUserId.DataBindings.Add("Text", bs, "UserId");

            txtFullName.DataBindings.Clear();
            txtFullName.DataBindings.Add("Text", bs, "FullName");

            rbSex.DataBindings.Clear();
            rbSex.DataBindings.Add("Checked", bs, "Sex");

            if (rbSex.Checked == true)
            {
                rbFemale.Checked = true;
                rbMale.Checked = false;
            }
            else
            {
                rbFemale.Checked = false;
                rbMale.Checked = true;
            }

            txtAddress.DataBindings.Clear();
            txtAddress.DataBindings.Add("Text", bs, "Address");

            txtEmail.DataBindings.Clear();
            txtEmail.DataBindings.Add("Text", bs, "Email");

            txtUserName.DataBindings.Clear();
            txtUserName.DataBindings.Add("Text", bs, "UserName");

            txtSalary.DataBindings.Clear();
            txtSalary.DataBindings.Add("Text", bs, "Salary");

            txtRemark.DataBindings.Clear();
            txtRemark.DataBindings.Add("Text", bs, "Remark");

            bn.BindingSource = bs;
            dgv.DataSource = bs;
        }

        public DataTable GetAll()
        {
            return m_UserDAO.GetAll();
        }

        public DataTable GetById(String userId)
        {
            return m_UserDAO.GetById(userId);
        }

        public DataTable GetByUsername(String userName)
        {
            return m_UserDAO.GetByUsername(userName);
        }

        public DataTable GetByFullName(String fullName)
        {
            return m_UserDAO.GetByFullName(fullName);
        }

        public DataTable GetByAddress(String address)
        {
            return m_UserDAO.GetByAddress(address);
        }

        public DataTable GetByPhone(String phone)
        {
            return m_UserDAO.GetByPhone(phone);
        }

        public DataTable GetByEmail(String email)
        {
            return m_UserDAO.GetByEmail(email);
        }

        public DataTable GetByGroup(String userGroupId)
        {
            return m_UserDAO.GetByGroup(userGroupId);
        }

        public bool UserHasGroup(String userId)
        {
            return m_UserDAO.UserHasGroup(userId);
        }

        public bool ExistUserByUserName(String userName)
        {
            return m_UserDAO.ExistUserByUserName(userName);
        }


        public bool ExistUserByUserNameAndPassword(String userName, String passWord)
        {
            return m_UserDAO.ExistUserByUserNameAndPassword(userName, passWord);
        }


        public DataTable Wellcome(String userId)
        {
            return m_UserDAO.Wellcome(userId);
        }

        public DataTable GetUserGroup(String userId)
        {
            return m_UserDAO.GetUserGroup(userId);
        }

        public int Add(UserInfo info)
        {
            return m_UserDAO.Add(info);
        }


        public int Update(UserInfo info)
        {
            return m_UserDAO.Update(info);
        }

        public int UpdateNoPass(UserInfo info)
        {
            return m_UserDAO.UpdateNoPass(info);
        }

        public int UpdateProfile(UserInfo info)
        {
            return m_UserDAO.UpdateProfile(info);
        }

        public int ChangePassword(UserInfo info)
        {
            return m_UserDAO.ChangePassword(info);
        }

        public string GetIdByUsername(String userName)
        {
            return m_UserDAO.GetIdByUsername(userName).ToString();
        }

        public int Delete(String userId)
        {
            return m_UserDAO.Delete(userId);
        }

        public int AuthenticationResult(String userName, String passWord)
        {
            passWord = Function.getMD5Hash(passWord);
            DataTable userCheck = GetByUsername(userName);

            if (userCheck.Rows.Count <= 0)
            {
                return 0; //User not exist !
            }
            else
            {
                bool existUserWithUsernameAndPassword = ExistUserByUserNameAndPassword(userName, passWord);
                String userId = userCheck.Rows[0]["UserId"].ToString();

                if (!existUserWithUsernameAndPassword)
                {
                    return 1; //Password invalid !
                }
                else
                {
                    bool userHasGroup = UserHasGroup(userId);
                    if (!userHasGroup)
                    {
                        return 2; //User not have any Group
                    }
                    else
                    {
                        GeneralInfo.userHasGroup = 3;
                        GeneralInfo.userId = userCheck.Rows[0]["UserId"].ToString();
                        GeneralInfo.userName = userCheck.Rows[0]["UserName"].ToString();
                        GeneralInfo.passWord = userCheck.Rows[0]["PassWord"].ToString();
                        GeneralInfo.fullName = userCheck.Rows[0]["FullName"].ToString();
                        DataTable group = GetUserGroup(userId);
                        GeneralInfo.userGroupId = group.Rows[0]["UserGroupId"].ToString();
                        GeneralInfo.userGroupName = group.Rows[0]["UserGroupName"].ToString();
                        return 3;
                    }
                }
            }
        }

        public DataRow AddNewRow()
        {
            return m_UserDAO.AddNewRow();
        }

        public void AddNewUser(DataRow m_Row)
        {
            m_UserDAO.AddNewUser(m_Row);
        }

        public bool SaveData()
        {
            return m_UserDAO.SaveData();
        }
    }
}
